package com.google.gwt.gfx.client.layers;

import com.google.gwt.gfx.client.GFXPanel;
import com.google.gwt.gfx.client.services.RefreshService;
import com.google.gwt.gfx.client.services.Refreshable;
import com.google.gwt.gfx.client.style.DefaultStyle;
import com.google.gwt.gfx.client.style.HasStyle;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;

/**
 * A type of {@link Panel} that represents layers.
 * 
 * A {@link Layer} is a layer for rendering content. CSS property "z-index" of a
 * {@link Layer} is used as rendering order in the {@link GFXPanel}.
 * 
 */
public abstract class Layer<S extends RefreshService<?>> extends ComplexPanel
		implements HasStyle, Refreshable<S> {

	private S serviceRef;

	private DefaultStyle styleRef;

	protected Layer() {
		super();

		setElement(DOM.createDiv());
		addStyleName("gfx-Layer");

		styleRef = new DefaultStyle(this);
	}

	@Override
	public void add(Widget widget) {
		super.add(widget, getElement());
	}

	@Override
	public void bind(final S service) {
		if (serviceRef != null) {
			removeStyleName(serviceRef.toString());
		}
		serviceRef = service;
		if (serviceRef != null) {
			addStyleName(serviceRef.toString());
		}
	}

	@Override
	public S service() {
		return serviceRef;
	}

	@Override
	public DefaultStyle style() {
		return styleRef;
	}

}
